package com.zm.zmgz.service;


import com.zm.zmgz.dao.AuthorityBeanMapper;
import com.zm.zmgz.pojo.AuthorityBean;
import com.zm.zmgz.pojo.AuthorityBeanExample;
import com.zm.zmgz.utils.RegularUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.AuthenticationException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AuthorityService {

    @Autowired
    AuthorityBeanMapper authorityBeanMapper;

    public List<String> getAuthoritys(String identificationnumber) { // 根据身份证返回权限列表
        AuthorityBeanExample example = new AuthorityBeanExample();
        AuthorityBeanExample.Criteria criteria = example.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationnumber);
        List<AuthorityBean> authorityBeans = authorityBeanMapper.selectByExample(example);
        List<String> stringList = new ArrayList<>();
        for (AuthorityBean authorityBean : authorityBeans) {
            stringList.add(authorityBean.getPath());
        }

        // 解决模块加载缺少问题
        /*
         * s格式类型只有3种:*,/dashboard,/employee/employeeInformation
         * */
        List<String> moduleArray = new ArrayList<>();
        for (String s : stringList) {
            moduleArray.addAll(RegularUtils.getAllPath(s,new ArrayList<String>()));
        }
        System.out.println("需要添加的未去重的模块:"+moduleArray.toString());
        stringList.addAll(moduleArray);
        System.out.println("未去重的权限:"+stringList);
        moduleArray.clear();
        for (String s : stringList) {
            if(!moduleArray.contains(s)){
                moduleArray.add(s);
            }
        }
        stringList = moduleArray;
        System.out.println("去重后的权限:"+stringList);
        return stringList;
    }

    /*
    * 参考sql:select * from authorityzm
                where identificationNumber = '440229199310244219'
	            and path LIKE '/employee/employeeInformation/_%'
    * */
    public List<String> getButtonAuthority(String identificationNumber, String path) { // 获取path页面按钮权限数组
        AuthorityBeanExample example = new AuthorityBeanExample();
        AuthorityBeanExample.Criteria criteria = example.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationNumber).andPathLike(path+"/_%");
        List<AuthorityBean> list = authorityBeanMapper.selectByExample(example);
        System.out.println("匹配到的按钮数组:"+list);
        List<String> stringList = new ArrayList<>();
        for (AuthorityBean authorityBean : list) {
            String[] split = authorityBean.getPath().split(path + "/");
//            System.out.println("权限按钮:"+split[1]);
            stringList.add(split[1]);
        }
        return stringList;
    }

    public List<AuthorityBean> getAuthority(String identificationnumber, String value) { // 获取一个AuthorityBean对象,虽然会返回一个集合
        AuthorityBeanExample example = new AuthorityBeanExample();
        AuthorityBeanExample.Criteria criteria = example.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationnumber).andPathEqualTo(value);
        List<AuthorityBean> list = authorityBeanMapper.selectByExample(example);
        System.out.println("根据身份证和path查询到的数据:"+list);
        return list;
    }
}
